package cn.cqs;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.*;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;

/**
 * shiro加密和解密
 */
public class Secrete {
    public static void main(String[] args) {
        String name="helloword";
//        使用Base64编码成字符串
       String base64encdes= Base64.encodeToString(name.getBytes());
        System.out.println("编码："+base64encdes);
        String base64Decodes = Base64.decodeToString(base64encdes.getBytes());
        System.out.println("解码："+base64Decodes);
//        ---------------使用HEX16进制字符串------------
        String hexencodes=Hex.encodeToString(name.getBytes());
        System.out.println("编码："+hexencodes);
        byte [] hexDecodes = Hex.decode(hexencodes);
//        byte[]数据 转 字符串String
        String st = new String(hexDecodes);
        String st2=CodecSupport.toString(hexDecodes);
        System.out.println("觖码："+st+"\t使用CodecSupport:"+st2);
//        ---------------------使用MD5---------------------
//        定义加盐
        String slat = "12345";
//        通过MD5Hash
         Md5Hash md5=new Md5Hash(name,slat,1);
         String  md5String=md5.toString();
        System.out.println("md5+盐算法："+md5String);
        System.out.println("md5+盐+hex:"+md5.toHex());
        System.out.println("md5+盐+base64:"+md5.toBase64());
//        方案二：
        String algorithmName1="md5";
        SimpleHash simpleHash1 = new SimpleHash(algorithmName1,name,slat);
        System.out.println("方案2：md5-1+盐："+simpleHash1.toHex());
//        --------------使用SHA-1------------------------
        String algorithmName="SHA-1";
        SimpleHash simpleHash = new SimpleHash(algorithmName,name,slat);
        System.out.println("SHA-1+盐："+simpleHash.toHex());
//        ---------------使用SHA-256-------------
        String algorithmName2="SHA-256";
        SimpleHash simpleHash2 = new SimpleHash(algorithmName2,name,slat);
        System.out.println("SHA-256+盐："+simpleHash2.toHex());
        Sha256Hash sha256Hash = new Sha256Hash(name,slat);
        System.out.println("方案2：SHA-256+盐："+sha256Hash.toString());
//        ---------------使用SHA-512-------------
        String algorithmName3="SHA-512";
        SimpleHash simpleHash3 = new SimpleHash(algorithmName3,name,slat);
        System.out.println("SHA512+盐："+simpleHash3.toHex());
        Sha512Hash sha512Hash = new Sha512Hash(name,slat);
        System.out.println("方案2：SHA-512+盐："+sha512Hash.toString());
        System.out.println("方案三：-------------SHA512-----------------");
        DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
        hashService.setPrivateSalt(new SimpleByteSource(slat)); //私盐，默认无
        hashService.setGeneratePublicSalt(true);//是否生成公盐，默认false
        hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
//        hashService.setHashIterations(1); //生成Hash值的迭代次数
        HashRequest request512=new HashRequest.Builder().setSource(name).setSalt(ByteSource.Util.bytes(slat)).setIterations(1).build();
        String hex512 = hashService.computeHash(request512).toHex();
        System.out.println("方案四：512算法：defaultHaxServie:"+hex512);
//        重写使用md5
        HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes(name))
                .setSalt(ByteSource.Util.bytes(slat)).setIterations(1).build();
        String hex = hashService.computeHash(request).toHex();
        System.out.println("重写使用MD5算法：defaultHaxServie:"+hex);


    }
}
